﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Mvc;
using ThaireInvestmentWeb.Util;
using ThaireInvestmentWeb.WcfSerivce;

namespace ThaireInvestmentWeb.Models
{
    public class EditLoanHeaderModel
    {
        public LoanHeaderScreenData ScreenData { get; set; }
        public LoansHeader Header { get; set; }

        public EditLoanHeaderModel()
        {
            Init();
        }
        void Init()
        {
            if (null == Header)
                Header = new LoansHeader();

            var defaultItem = WebUtil.SelectListSelectDefault();
            CompanyList = new BindingList<SelectListItem>() { defaultItem };
            InvolvedPartyTypeNameList = new BindingList<SelectListItem>() { defaultItem };
            LoanTypeList = new BindingList<SelectListItem>() { defaultItem };
            ObjectiveList = new BindingList<SelectListItem>() { defaultItem };
            GuaranteeTypeList = new BindingList<SelectListItem>() { defaultItem };
            AssessmentMortgateList = new BindingList<SelectListItem>() { defaultItem };
            OccupationsOfBorrowerList = new BindingList<SelectListItem>() { defaultItem };
            OccupationsOfGuarantorList = new BindingList<SelectListItem>() { defaultItem };
            ProvinceList = new BindingList<SelectListItem>() { defaultItem };
            DistrictList = new BindingList<SelectListItem>() { defaultItem };
            SubDistrictList = new BindingList<SelectListItem>() { defaultItem };
            ////data.Owners
            
        }
        public static EditLoanHeaderModel BuildModel(LoanHeaderScreenData data)
        {
            var model = new EditLoanHeaderModel { ScreenData = data };
            if (data.HeaderDTO != null)
            {
                if (data.HeaderDTO.Header != null)
                    model.Header = data.HeaderDTO.Header;
                ////todo: ขาดการ assign value 
                //if (data.HeaderDTO.LoansExtrainstallments != null)
                //    model.LoansExtrainstallments = data.HeaderDTO.LoansExtrainstallments;
            }
            if (data.Companies != null && data.Companies.Any())
            {
                foreach (var selectListItem in data.Companies)
                {
                    model.CompanyList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.CompanyNameTh, Value = selectListItem.CompanyId.ToString() });
                }
            }
            if (data.InvolvedPartyTypes != null && data.InvolvedPartyTypes.Any())
            {
                foreach (var selectListItem in data.InvolvedPartyTypes)
                {
                    model.InvolvedPartyTypeNameList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.InvolvedPartyTypeName, Value = selectListItem.InvolvedPartyTypeId.ToString() });
                }
                if (model.Header != null)
                {
                    var a = model.ScreenData.InvolvedPartyTypes.SingleOrDefault(x => x.InvolvedPartyTypeId == model.Header.InvolvedPartyTypeId);
                    if (a != null)
                    {
                        model.InvolvedPartyTypeReferance = a.InvolvedPartyTypeReference;
                        model.InvolvedPartyTypeNo = a.InvolvedPartyTypeCode;
                    }
                }
            }
            if (data.LoanTypes != null && data.LoanTypes.Any())
            {
                foreach (var selectListItem in data.LoanTypes)
                {
                    model.LoanTypeList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.Key, Value = selectListItem.ValueEN });
                }
            }
            if (data.Objectives != null && data.Objectives.Any())
            {
                foreach (var selectListItem in data.Objectives)
                {
                    model.ObjectiveList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.Key, Value = selectListItem.ValueEN });
                }
            }
            if (data.GuaranteeTypes != null && data.GuaranteeTypes.Any())
            {
                foreach (var selectListItem in data.GuaranteeTypes)
                {
                    model.GuaranteeTypeList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.Key, Value = selectListItem.ValueEN });
                }
            }
            if (data.AssessmentMortgates != null && data.AssessmentMortgates.Any())
            {
                foreach (var selectListItem in data.AssessmentMortgates)
                {
                    model.AssessmentMortgateList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.Key, Value = selectListItem.ValueEN });
                }
            }
            if (data.Occupations != null && data.Occupations.Any())
            {
                foreach (var selectListItem in data.Occupations)
                {
                    model.OccupationsOfBorrowerList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.OccupationName, Value = selectListItem.OccupationId.ToString() });
                    model.OccupationsOfGuarantorList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.OccupationName, Value = selectListItem.OccupationId.ToString() });
                }
            }
            return model;
        }

        #region dropdown list


        [Required(ErrorMessage = "โปรดระบุ")]
        public int? SelectedCompany
        {
            get { return Header.CompanyID; }
            set { Header.CompanyID = value; }
        }
        public IList<SelectListItem> CompanyList { get; set; }

        [Required(ErrorMessage = "โปรดระบุ")]
        public int? SelectedLoanType
        {
            get { return Header.LoanTypeID; }
            set { Header.LoanTypeID = value; }
        }
        public IList<SelectListItem> LoanTypeList { get; set; }

        public int? SelectedObjective
        {
            get { return Header.LoanObjectiveID; }
            set
            {
                Header.LoanObjectiveID = value;
            }
        }
        public IList<SelectListItem> ObjectiveList { get; set; }


        public int? SelectedOccupationsOfBorrower
        {
            get { return Header.BorrowerOccupationID; }
            set
            {
                Header.BorrowerOccupationID = value;
            }
        }
        public IList<SelectListItem> OccupationsOfBorrowerList { get; set; }

        /// <summary>
        /// อาชีพ ผู้ค้ำประกัน
        /// </summary>
        public int? SelectedOccupationsOfGuarantor
        {
            get { return Header.GuarantorOcuupationID; }
            set
            {
                Header.GuarantorOcuupationID = value;
            }
        }
        public IList<SelectListItem> OccupationsOfGuarantorList { get; set; }

        /// <summary>
        /// ประเภทหลักประกัน 
        /// </summary>
        [Required(ErrorMessage = "โปรดระบุ")]
        public int? SelectedGuaranteeType
        {
            get { return Header.GuaranteeTypeID; }
            set
            {
                Header.GuaranteeTypeID = value;
            }
        }
        public IList<SelectListItem> GuaranteeTypeList { get; set; }

        /// <summary>
        /// รายละเอียดทรัพย์สินที่ทำจำนอง  
        /// </summary>
        public int? SelectedAssessmentMortgate
        {
            get { return Header.AssessmentMortgateID; }
            set
            {
                Header.AssessmentMortgateID = value;
            }
        }
        public IList<SelectListItem> AssessmentMortgateList { get; set; }



        /// <summary>
        /// จังหวัด
        /// </summary>
        public int? SelectedProvince
        {
            get { return Header.TitleDeedProvinceID; }
            set
            {
                Header.TitleDeedProvinceID = value;
            }
        }
        public IList<SelectListItem> ProvinceList { get; set; }

        /// <summary>
        /// อำเภอ
        /// </summary>
        public int? SelectedDistrict
        {
            get { return Header.TitleDeedDistrictID; }
            set
            {
                Header.TitleDeedDistrictID = value;
            }
        }
        public IList<SelectListItem> DistrictList { get; set; }

        /// <summary>
        /// ตำบล
        /// </summary>
        public int? SelectedSubDistrict
        {
            get { return Header.TitleDeedSubDistrictID; }
            set
            {
                Header.TitleDeedSubDistrictID = value;
            }
        }
        public IList<SelectListItem> SubDistrictList { get; set; }

        public int? SelectedInvolvedPartyType
        {
            get { return Header.InvolvedPartyTypeId; }
            set
            {
                Header.InvolvedPartyTypeId = value;
            }
        }
        public IList<SelectListItem> InvolvedPartyTypeNameList { get; set; }


        #endregion

        #region adapter
        public int HeaderID
        {
            get
            {
                return Header.LoansHeaderID;
            }
            set
            {
                Header.LoansHeaderID = value;
            }
        }
        [Required(ErrorMessage = "โปรดระบุ")]
        public string BorrowerName
        {
            get { return Header.BorrowerName; }
            set { Header.BorrowerName = value; }
        }
        [Required(ErrorMessage = "โปรดระบุ")]
        public string ContractNumber
        {
            get { return Header.ContractNumber; }
            set { Header.ContractNumber = value; }
        }
    

        public String InvolvedPartyTypeReferance { get; set; }
        public String InvolvedPartyTypeNo { get; set; }

        private bool _isTierISelected;
        public bool IsTierISelected
        {
            get { return _isTierISelected; }
            set
            {
                _isTierISelected = value;
            }
        }

        private bool _isTierIiSelected;
        public bool IsTierIISelected
        {
            get { return _isTierIiSelected; }
            set
            {
                _isTierIiSelected = value;
            }
        }

        private bool _isLowLiquiditySelected;
        public bool IsLowLiquiditySelected
        {
            get { return _isLowLiquiditySelected; }
            set
            {
                _isLowLiquiditySelected = value;
            }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public DateTime? ContractStartDate
        {
            get { return Header.ContractStartDate; }
            set { Header.ContractStartDate = value; }
        }
        [Required(ErrorMessage = "โปรดระบุ")]
        public DateTime? ContractEndDate
        {
            get { return Header.ContractEndDate; }
            set { Header.ContractEndDate = value; }
        }
        [Required(ErrorMessage = "โปรดระบุ")]
        public decimal? ContractAmount
        {
            get { return Header.ContractAmount; }
            set { Header.ContractAmount = value; }
        }
        #endregion

        public String SaveBtn { get; set; }
        public String CancelBtn { get; set; }
        public String EditBtn { get; set; }
        public bool UpdateMode { get; set; }
    }

}